package com.gxs.tga.poc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxs.tga.poc.domain.TgaSolution;
import com.gxs.tga.poc.domain.TgaSolutionTP;
import com.gxs.tga.poc.domain.exceptions.DuplicateTgaSolution;
import com.gxs.tga.poc.domain.exceptions.DuplicateTgaSolutionTP;
import com.gxs.tga.poc.domain.exceptions.TgaSolutionNotFound;
import com.gxs.tga.poc.domain.exceptions.TgaSolutionTPNotFound;
import com.gxs.tga.poc.repository.TgaSolutionRepository;
import com.gxs.tga.poc.repository.TgaSolutionTPRepository;

@Service
public class TgaSolutionTPServiceImpl implements TgaSolutionTPService
{

	@Resource
	private TgaSolutionTPRepository tgaSolutionTPRepository;

	@Override
	@Transactional
	public TgaSolutionTP create(TgaSolutionTP tradingPartner) throws DuplicateTgaSolutionTP
	{
		TgaSolutionTP existingTP = this.findById(tradingPartner.getTpRecordId());
		if(existingTP != null)
		{
			throw new DuplicateTgaSolutionTP();
		}
		TgaSolutionTP createdPartner = tradingPartner;
		
		return tgaSolutionTPRepository.save(createdPartner);
	}

	@Override
	@Transactional
	public TgaSolutionTP findById(String tpRecordId)
	{
		return tgaSolutionTPRepository.findOne(tpRecordId);
	}

	@Override
	@Transactional(rollbackFor = TgaSolutionTPNotFound.class)
	public TgaSolutionTP delete(String tpRecordId) throws TgaSolutionTPNotFound
	{
		TgaSolutionTP deletedPartner = tgaSolutionTPRepository.findOne(tpRecordId);

		if (deletedPartner == null)
			throw new TgaSolutionTPNotFound();

		tgaSolutionTPRepository.delete(deletedPartner);
		return deletedPartner;
	}

	@Override
	@Transactional
	public List<TgaSolutionTP> findAll()
	{
		return tgaSolutionTPRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor=TgaSolutionTPNotFound.class)
	public TgaSolutionTP update(TgaSolutionTP partner) throws TgaSolutionTPNotFound 
	{
		TgaSolutionTP updatedPartner = tgaSolutionTPRepository.findOne(partner.getTpRecordId());
		
		if (updatedPartner == null)
			throw new TgaSolutionTPNotFound();
		
		updatedPartner.setCompId(partner.getCompId());
		updatedPartner.setTgaSolution(partner.getTgaSolution());
		
		return updatedPartner;
	}

	@Override
	@Transactional
	public List<TgaSolutionTP> findByCompId(String compId)
	{
		return tgaSolutionTPRepository.findByCompId(compId);	
	}

	@Override
	@Transactional
	public List<TgaSolutionTP> findByCompIdAndTgaSolution(String compId, TgaSolution tgaSolution)
	{
		return this.tgaSolutionTPRepository.findByCompIdAndTgaSolution(compId, tgaSolution);
	}

	@Override
	@Transactional
	public List<TgaSolutionTP> findByTgaSolution(TgaSolution tgaSolution)
	{
		return this.tgaSolutionTPRepository.findByTgaSolution(tgaSolution);
	}
}
